<?php
define ('MINF_PATH', dirname(__FILE__)."/");
if ($_SERVER["SCRIPT_FILENAME"]) define ('USER_PATH', dirname($_SERVER["SCRIPT_FILENAME"])."/");
elseif ($_ENV["SCRIPT_FILENAME"]) define ('USER_PATH', dirname($_ENV["SCRIPT_FILENAME"])."/");

/*
	variables :
		$minf_start			The time minf_init() has been called
		$conf				Minf Configuration
		$cl					Client Request Data
*/

function minf_init() {
	//global $minf_start, $conf, $cl, $urlconf, $urldata;
	global $root;
	$conf = &$GLOBALS['conf'];
	$cl = &$GLOBALS['cl'];
	$minf_start = &$GLOBALS['minf_start'];
	$urlconf = &$GLOBALS['urlconf'];
	$urldata = &$GLOBALS['urldata'];
	$tocall = &$GLOBALS['apptocall'];
	
	ob_start();										// SUPRESS OUTPUT
	$minf_start = microtime(true);					// GET START TIME
	
	error_reporting(E_ALL ^ E_NOTICE);				// WE WANT ERROR OUTPUT
	ini_set('display_errors', 1);

	include_once("core/functions.php");				// INCLUDE NEEDED FILES
	
	echosys("SYS: Including Core Modules", MSG_NOTICE);
	includeAll(MINF_PATH."core/modules/");
	
	echosys("SYS: Scanning Core Applications", MSG_NOTICE);
	App::Scan(MINF_PATH."core/apps/");
	
	echosys("SYS: Parsing main Config", MSG_NOTICE);
	$conf = parseCfgFile(MINF_PATH."minf.conf");	// GET CONFIGURATION
	
	echosys("SYS: Detecting Request", MSG_NOTICE);
	$cl = user_request();							// DETECT USER REQUEST
	
	if (file_exists($cl["rootpath"]."/minf.conf"))	// GET USER OVERRIDES FOR CONFIGURATION
		$conf = parseCfgFile(USER_PATH."/minf.conf", $conf);
	if (is_array($conf["files"]["plug"]))
		foreach ($conf["files"]["plug"] as $avf)		// INCLUDE OPTIONAL FILES
			App::RegisterApp(MINF_PATH."available/".$avf);

	echosys("SYS: Including All User Libraries in ".$conf["userdir"]["lib"], MSG_NOTICE);
	includeAll($cl["rootpath"].$conf["userdir"]["lib"]);	// INCLUDE ALL USER LIBRARIES
	echosys("SYS: Scanning User Applications in ".$conf["userdir"]["app"], MSG_NOTICE);
	App::Scan($cl["rootpath"].$conf["userdir"]["app"]);		// CALL ALL USER APPLICATIONS
	
	$conf["url"] = sortArrayByField($conf["url"], "priority");	// SORT URL SCHEMAS /by PRIORITY
	
	foreach ($conf["url"] as $u) {								// TEST THEM ALL
		$pcre = schema2pcre($u["schema"]);
		if ($data = parseUrl($cl["linkreq"], $pcre)) {	// FIRST MATCHING SCHEMA
			$urlconf = $u;
			$urldata = $data;
			$tocall = formAppCall();						// $tocall WHAT TO CALL
			if (App::Check($tocall['app'], $tocall['call'])) break;
			echosys("URL: Nonmatch: ".$u['schema']."  Priority:".$u["priority"], MSG_WARNING, "Because no such app/call<br/>".htmlspecialchars($pcre[0]));
		}else{
			echosys("URL: Nonmatch: ".$u['schema']."  Priority:".$u["priority"], MSG_WARNING, htmlspecialchars($pcre[0]));
		}
	}
	if (!$urlconf) fatal("Cannot Find any Matching URL-Schema");
	if (App::Check($tocall['app'], $tocall['call'])&&$urlconf) {
		echosys("URL: Matched : ".$urlconf['schema']."  Priority:".$urlconf["priority"], MSG_NOTICE, htmlspecialchars($pcre[0]));
	}else{
		echosys("ROUTE: URL Match found, but targeting application/call is not found", MSG_ERROR, $tocall['app']."::".$tocall['call']."() not found, continueing on other calls");
	}
	
	if ($conf["userdir"]["js"]) includeAllJS($conf["userdir"]["js"]);
	if ($conf["userdir"]["jsplugin"]) includeAllJS($conf["userdir"]["jsplugin"], true);
	
	$root = $cl['proto']."://".$cl['domain'].$cl['portadd'].$cl['relurl'];
	// TEMPLATE VARS
	Page::Set("tp", $root.$conf["userdir"]["tpl"]);
	Page::Set("r", $root);
	Page::Set("l", $tocall['lang']);
	Page::Set("rl", $root."/".$tocall['lang']);
	Page::Set("timestamp", time());
	Page::Set("url", url());
	
	if (!App::$Calls["regular"]) {
		echo "Cannot find any application or any 'regular' call<br />";
		echo "<br />Maybe you should create and application in ".$conf['userdir']['app'];
		die();
	}
		
	App::Call("Index", "Startup", $tocall);			// CALL STARTUP

	foreach (App::$Calls["regular"] as $_app => $_appc) 	// CALL Init on EVERY APP
		App::Call($_app, "Init", $tocall);
	
	App::CallSeq($tocall['app'], $tocall['call'], $tocall['argv']);		// CALL APPLICATION
	
	foreach (App::$Calls["regular"] as $_app => $_appc)		// CALL unInit on EVERY APP
		App::Call($_app, "unInit", $tocall);
		
	App::Call("Index", "Endup", $tocall);			// CALL ENDUP
}

function root() { global $root; return $root; }
function rootpath() { global $cl; return $cl['rootpath']; }
function tp() { global $conf; return $conf['userdir']['tpl']; }
function url() { global $cl; return $cl["proto"]."://".$cl["domain"].$cl["portadd"].$cl["relurl"]."/".$cl['linkreq']; }
function lang() { global $apptocall; return $apptocall['lang']; }
function target_app() { global $apptocall; return $apptocall['app']; }
function target_call() { global $apptocall; return $apptocall['call']; }
function target_argv() { global $apptocall; return $apptocall['argv']; }
function target_argc() { global $apptocall; return $apptocall['argc']; }

?>